package com.lv.phuotbacnam.ui.adapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.queries.QueriesConstant.TableUser;
import com.lv.phuotbacnam.ui.fragment.MyItem;

import com.lv.phuotbacnam.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.parse.ParseObject;
//import android.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HorizontalListViewAdapter extends BaseAdapter
{

	private ArrayList<ParseObject> arrItem;
	private Context context;
	
	public HorizontalListViewAdapter(ArrayList<ParseObject> arrItem, Context context) {
		super();
		this.arrItem = arrItem;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arrItem.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arrItem.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
            //LayoutInflater mInflater = (LayoutInflater) context
            //        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            //convertView = mInflater.inflate(R.layout.this,null);
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_listview, null);
		}
		
		ImageView img = (ImageView)convertView.findViewById(R.id.image_item);
		//img.setImageResource(R.drawable.nhatrang);
		ParseObject diadiem = arrItem.get(position);
		String image = "";
		JSONArray list_photo = diadiem.getJSONArray(QueriesConstant.TableDiaDiem.COLUMN_list_photo);
		try {
			JSONObject first_photo = list_photo.getJSONObject(0);
			image = first_photo.getString("url");	
			TextView tv = (TextView)convertView.findViewById(R.id.tv_item);
			
			tv.setText(diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem));
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		ImageLoader imageLoader;
		imageLoader = ImageLoader.getInstance();
		
		//cache image
		DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
						.cacheOnDisc(true).resetViewBeforeLoading(true).build();
		
        //Initialize ImageLoader with configuration. Do it once.
        imageLoader.init(ImageLoaderConfiguration.createDefault(parent.getContext()));
		imageLoader.displayImage(image,img,options);
		
		return convertView;
	}

	
}
